//
//  ZYChangeOilsPriceViewController.m
//  ZYOils
//
//  Created by 段新瑞 on 2019/9/30.
//  Copyright © 2019 Mr.Xr. All rights reserved.
//

#import "ZYChangeOilsPriceViewController.h"
#import "ZYRequestChangeOrAddOilsModel.h"

@interface ZYChangeOilsPriceViewController ()

@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextField *oldPriceTextfield;

@property (weak, nonatomic) IBOutlet UITextField *newsPriceTextField;

@property (nonatomic, strong) ZYRequestChangeOrAddOilsModel *requestModel;

@end

@implementation ZYChangeOilsPriceViewController

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configViews];
    [self configData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

#pragma mark - Setup View / Data

- (void)configViews {
    [self.nameTextField becomeFirstResponder];
    
    [LBLimitInput limitInputView:self.oldPriceTextfield regX:@"^\\-?([0-9]\\d{0,5})(\\.\\d{0,2})?$"];
    [LBLimitInput limitInputView:self.newsPriceTextField regX:@"^\\-?([0-9]\\d{0,5})(\\.\\d{0,2})?$"];
}

- (void)configData {
    self.requestModel.companyId = ZYUserInfoModel.sharedInfoModel.userModel.companyId;
    self.nameTextField.text = self.model.oilName;
    self.oldPriceTextfield.text = self.model.oldPrice;
    self.newsPriceTextField.text = self.model.neePrice;
}

#pragma mark - Observer

#pragma mark - Notification

#pragma mark - Event Response
- (IBAction)confirmButtonClick:(id)sender {
    [self.view endEditing:YES];
    self.requestModel.oilName = self.nameTextField.text;
    self.requestModel.oldPrice = self.oldPriceTextfield.text;
    self.requestModel.myNewPrice = self.newsPriceTextField.text;
    if ([LBLimitInput isEmpty:self.requestModel.oilName]) {
        [ZYHud showErrorText:@"名称不能为空"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.requestModel.oldPrice]) {
        [ZYHud showErrorText:@"原价不能为空"];
        return;
    }
    
    if ([LBLimitInput isEmpty:self.requestModel.myNewPrice]) {
        [ZYHud showErrorText:@"现价不能为空"];
        return;
    }
    [ZYHud showLoadingText:@""];
    if (self.state == OilsStateAdd) {
        /// 添加
        [[DataManager manager] postAddOilParam:self.requestModel.mj_keyValues block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            
            if (model.isSuccess) {
                [ZYHud showSuccessText:@"油品添加成功"];
                @weakify(self)
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    @strongify(self)
                    [self.subject sendNext:@"add"];
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    } else {
        self.requestModel.oilId = self.model.oilId;
        /// 修改
        [[DataManager manager] postUpdateOilParam:self.requestModel.mj_keyValues block:^(XRCommonModel *model, NSError *error) {
            [ZYHud hideHud];
            
            if (model.isSuccess) {
                [ZYHud showSuccessText:@"油品修改成功"];
                @weakify(self)
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    @strongify(self)
                    self.model.oilName = self.requestModel.oilName;
                    self.model.oldPrice = self.requestModel.oldPrice;
                    self.model.neePrice = self.requestModel.myNewPrice;
                    [self.subject sendNext:@"change"];
                    [self.navigationController popViewControllerAnimated:YES];
                });
            } else {
                if (![LBLimitInput isEmpty:model.error]) {
                    [ZYHud showErrorText:model.error];
                }
            }
        }];
    }
}

#pragma mark - Override Methods

#pragma mark - Delegate

#pragma mark - Public Methods

#pragma mark - Private Methods

#pragma mark - Setter / Getter

- (ZYRequestChangeOrAddOilsModel *)requestModel {
    if (!_requestModel) {
        _requestModel = [[ZYRequestChangeOrAddOilsModel alloc] init];
    }
    return _requestModel;
}

- (RACSubject *)subject {
    if (!_subject) {
        _subject = [RACSubject subject];
    }
    return _subject;
}

#pragma mark - Network




@end
